//
//  ST_NearbyGirlsRegisterController.swift
//  btvideo
//
//  Created by mac on 2020/9/25.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyAttributes
import RealmSwift
import RxCocoa
import RxSwift


class ST_NearbyGirlsRegisterController: UIViewController,ST_RegisterRouteProtocol{
    
 
    
    
    func city_observer(cityStr: String) {
        
        
        
        self.setup_content_label(city: cityStr)
        
    }
    
    
    fileprivate func setup_content_label(city:String?){
        
        let str = NSMutableAttributedString()
   
        if let c = city{
            
            str.append("当前在\(c)有\n".withAttributes(normal_attribute))
            
        }else{
            str.append("当前有\n".withAttributes(normal_attribute))
        }
        
        
        if let number = ST_RegisterMonitor.shared.regist_response_account()?.userinfo?.total_num,number > 1{
            str.append(number.string.withAttributes(highlight_attribute))
       }else{
           str.append("许多个".withAttributes(highlight_attribute))
       }
        
        
        var genderStr = "小姐姐"
        if let sex = ST_RegisterMonitor.shared.regist_response_account()?.userinfo?.sex,sex == .female{
            genderStr = "小哥哥"
        }
        
        str.append("个超甜美\(genderStr)\n我们将为你提供特质的挑选\n这有助于我们为你找到更合适的人选".withAttributes(normal_attribute))
        
        let paragrahp = NSMutableParagraphStyle.create_line_spacing_paragraph_style(lineSpacing: 5)
        
        paragrahp.alignment = .center
        
        str.addAttributes([Attribute.paragraphStyle(paragrahp)], range: NSMakeRange(0, str.length))
        
        content_label.attributedText = str
        
    }
    
    fileprivate let normal_attribute:[Attribute] = {
        return [Attribute.textColor(Color.init(hex: 0x333333)!),Attribute.font(Font.systemFont(ofSize: 16, weight: UIFont.Weight.regular))]
    }()
    
    
    fileprivate let highlight_attribute:[Attribute] = {
        return [Attribute.textColor(Color.init(hex: 0xFEC207)!),Attribute.font(Font.systemFont(ofSize: 16, weight: UIFont.Weight.semibold))]
    }()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = Color.white
        
        view.addSubview(animation_back_view)
        view.addSubview(animationView)
        
        
        animationView.snp.makeConstraints { (make) in
            make.width.height.equalTo(314)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(statusBarHeight+40)
        }
        
        animation_back_view.snp.makeConstraints { (make) in
            make.edges.equalTo(animationView)
        }
        
        animationView.play { (f) in
            self.animation_back_view.play()
        }
        
        view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            
            make.centerX.equalToSuperview()
            make.top.equalTo(animationView.snp.bottom).offset(30)
            make.height.equalTo(30)
        }
        
        view.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            
            make.centerX.equalToSuperview()
            make.top.equalTo(animationView.snp.bottom).offset(75)
        }
        
        
        view.addSubview(start_btn)
        
        start_btn.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-BTSafeAreaBottom-25)
            make.centerX.equalToSuperview()
            make.width.equalTo(260)
            make.height.equalTo(44)
        }
        
        binding()
        
        back_thread_execute(afterDelay: 1) {
            ST_RegisterMonitor.shared.logger.report(fromController: self)
        }
    }
    private func binding(){
        
        
        SCComponets.location_component.city_observer.compactMap({ $0 }).withUnretained(self).subscribe { own,city in
            
            own.city_observer(cityStr: city)
            
        }.disposed(by: rx.disposeBag)
        
    }

//    MARK: - Properties
    var disposeBag = DisposeBag()
    
    
    fileprivate lazy var animationView:LOTAnimationView = {
        
        var resourceName = "registerShowFemale"
        if let sex = ST_RegisterMonitor.shared.regist_response_account()?.userinfo?.sex,sex == .female{
            resourceName = "registerShowMale"
        }
        
        let path = Bundle.main.path(forResource: resourceName, ofType: "json")!
        
        let control = LOTAnimationView.init(filePath: path)
        
        control.contentMode = UIView.ContentMode.scaleAspectFill
        
        //        control.loopAnimation = true
        
        return control
    }()
    
    
    fileprivate lazy var animation_back_view:LOTAnimationView = {
        
        let resourceName = "registerShowSomeoneLoop"
        
        let path = Bundle.main.path(forResource: resourceName, ofType: "json")!
        
        let control = LOTAnimationView.init(filePath: path)
        
        control.contentMode = UIView.ContentMode.scaleAspectFill
        
        control.loopAnimation = true
        
        return control
    }()
    
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        
        if let sex = ST_RegisterMonitor.shared.regist_response_account()?.userinfo?.sex,sex == .female{
            control.text = "为你推荐附近符合你要求的小哥哥"
        }else{
            control.text = "为你推荐附近符合你要求的小姐姐"
        }
        control.textColor = UIColor(hexString: "#333333")
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 22, weight: UIFont.Weight.semibold)
        return control
    }()
    
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.numberOfLines = 0
        return control
    }()
    
    
    fileprivate lazy var start_btn:UIButton = {
       
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        
        control.cornerRadius = 22

        control.setBackgroundImage(UIImage.init(color: UIColor.init(hex: 0xFEC207)!, size: CGSize.init(width: 1, height: 1)), for: UIControl.State.normal)
        
        control.setBackgroundImage(UIImage.init(color: UIColor.init(hex: 0xDFE3E9)!, size: CGSize.init(width: 1, height: 1)), for: UIControl.State.disabled)
        
        control.setTitleForAllStates("立即开始")
        
        control.setTitleColorForAllStates(UIColor.white)
        
        control.setTitleColor(UIColor.white, for: UIControl.State.disabled)
        
        control.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        
        control.addTarget(self, action: #selector(start_action), for: UIControl.Event.touchUpInside)
        
        return control
    }()
    
}


extension ST_NearbyGirlsRegisterController{
    
    @objc fileprivate func start_action(){
        
        
        self.start_btn.isUserInteractionEnabled = false
                
        main_thread_execute(afterDelay: 2) {
            self.start_btn.isUserInteractionEnabled = true
        }
        
        self.animationView.stop()
        
        self.animation_back_view.stop()
        
        //TianRegisterStepManager.shared.pushNextStepAction()
        
        pushNextRegisterController(parameters: nil)
    }
    
}
